package com.playball.lottery.nt;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.sessions.SessionManager;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.Logger;
import com.playball.lottery.util.MD5Encrypt;

import java.util.Map;


//获取服务请求参数
public class GetRequestParams {

	public static String Uuid = "3C075461B9B0";
	
	public static String getRequestSign(String uuid, String platformCode, String platformVersion, String appVersion, String params, String key) {
		String sign = uuid + platformCode + platformVersion + appVersion + params + key;
		sign = MD5Encrypt.md5Encryption(sign);
		return sign;
	}

	public static void setUUid(String uuid){
		if(TextUtils.isEmpty(uuid))
			Uuid = "3C075461B9B0";
		else
			Uuid = uuid;
	}
	
	// 发送请求
	public static String getRequestResult(String paramArr, String action, Context mContext) {
//		Logger.e("GetRequestParams ", "getRequestResult:"+" ,paramArr= "+paramArr+" ,action= "+action);
		String strResult = null;
		try {
			String sign = getRequestSign(Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
			JSONObject params = new JSONObject();
			params.put("uuid", Uuid);
			params.put("platformCode", "Android");
			params.put("platformVersion", android.os.Build.VERSION.RELEASE);
			params.put("appVersion", ApplicationData.APPVERSION);
			params.put("action", action);
			params.put("params", paramArr.replace("+", "%2B"));
			params.put("sign", sign);
			params.put("cmdId",ApplicationData.getInstance().getCommenderID());
			params.put("cmdName",ApplicationData.getInstance().getCommenderName());
			Logger.e("GetRequestParams ", "params = "+params.toString());
			strResult = GetJsonData.getInputStreamByPost(ConvertJsonToList.getMap(params.toString()), mContext, false);
			// byte[] data = GetJsonData.readStream(is);
			// strResult = new String(data);
			Logger.e("GetRequestParams", "strResult = " + strResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		return strResult;
	}

	// 发送请求
	public static String getRequestResultWithLSID(String paramArr, String action, Context mContext) {
//		Logger.e("GetRequestParams ", "getRequestResult:"+" ,paramArr= "+paramArr+" ,action= "+action);
		String strResult = null;
		try {
			String sign = getRequestSign(Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
			JSONObject params = new JSONObject();
			params.put("uuid", Uuid);
			params.put("platformCode", "Android");
			params.put("platformVersion", android.os.Build.VERSION.RELEASE);
			params.put("appVersion", ApplicationData.APPVERSION);
			params.put("action", action);
			params.put("params", paramArr.replace("+", "%2B"));
			params.put("sign", sign);
			params.put("cmdId",ApplicationData.getInstance().getCommenderID());
			params.put("cmdName",ApplicationData.getInstance().getCommenderName());
			Logger.e("GetRequestParams ", "params = "+params.toString());
			strResult = GetJsonData.getInputStreamByPost(ConvertJsonToList.getMap(params.toString()), mContext, false);
			// byte[] data = GetJsonData.readStream(is);
			// strResult = new String(data);
			Logger.e("GetRequestParams", "strResult = " + strResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		return strResult;
	}
	
	// 发送请求
	public static String getRequestResult(String paramArr, String action, Context mContext, boolean isold) {
		//Log.v("GetRequestParams ", "getRequestResult:"+" ,paramArr= "+paramArr+" ,action= "+action);
		String strResult = null;
		try {
			String sign = getRequestSign(Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
			JSONObject params = new JSONObject();
			params.put("uuid", Uuid);
			params.put("platformCode", "Android");
			params.put("platformVersion", android.os.Build.VERSION.RELEASE);
			params.put("appVersion", ApplicationData.APPVERSION);
			params.put("UserID", ApplicationData.getInstance().getUserID());
			params.put("action", action);
			params.put("params", paramArr.replace("+", "%2B"));
			params.put("sign", sign);
			params.put("cmdId",ApplicationData.getInstance().getCommenderID());
			params.put("cmdName",ApplicationData.getInstance().getCommenderName());
			Logger.e("GetRequestParams ", "getRequestResult params="+params.toString());
			strResult = GetJsonData.getInputStreamByPost(ConvertJsonToList.getMap(params.toString()), mContext, isold);
			// byte[] data = GetJsonData.readStream(is);
			// strResult = new String(data);
			Logger.e("getRequestResult", "strResult = " + strResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		return strResult;
	}
	
	// 发送请求
	public static String getRequestResultKjInfo(String paramArr, String action, Context mContext) {
		//Log.v("GetRequestParams ", "getRequestResultKjInfo:"+" ,paramArr= "+paramArr+" ,action= "+action);
		String strResult = null;
		try {
			String sign = getRequestSign(Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
			JSONObject params = new JSONObject();
			params.put("uuid", Uuid);
			params.put("platformCode", "Android");
			params.put("platformVersion", android.os.Build.VERSION.RELEASE);
			params.put("appVersion", ApplicationData.APPVERSION);
			params.put("action", action);
			params.put("params", "{}");
			params.put("sign", sign);
			params.put("cmdId",ApplicationData.getInstance().getCommenderID());
			params.put("cmdName",ApplicationData.getInstance().getCommenderName());
			Logger.e("getRequestResultKjInfo ", "getRequestResultKjInfo params="+params.toString());
			strResult = GetJsonData.getInputStreamByPost(ConvertJsonToList.getMap(params.toString()), mContext,true);
			Logger.e("getRequestResultKjInfo", "strResult = " + strResult);
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return strResult;
	}
	
	
	/**
	 * 发送带UserID的请求
	 * @param paramArr 参数
	 * @param UserID 登录用户名
	 * @param action 类型
	 * @param mContext 上下文
	 * @return
	 */
	public static String getRequestUserIDResult(String paramArr, String UserID,String action, Context mContext) {
//		Logger.e("GetRequestParams ", "getRequestUserIDResult:"+" ,paramArr= "+paramArr+" ,UserID= "+UserID+" ,action= "+action);
		String strResult = null;
		try {
			String sign = getRequestSign(Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
			JSONObject params = new JSONObject();
			params.put("uuid", Uuid);
			params.put("platformCode", "Android");
			params.put("platformVersion", android.os.Build.VERSION.RELEASE);
			params.put("appVersion", ApplicationData.APPVERSION);
			params.put("UserID", UserID);
			params.put("action", action);
			SessionManager sessionManager = new SessionManager(mContext);
			Map<String,String> userDetails = sessionManager.getUserDetails();
			params.put("LSID", userDetails.get(sessionManager.KEY_LSID));	// LSID
			params.put("params", paramArr.replace("+", "%2B"));
			params.put("sign", sign);
			params.put("cmdId",ApplicationData.getInstance().getCommenderID());
			params.put("cmdName",ApplicationData.getInstance().getCommenderName());
			Logger.e("GetRequestParams ", "getRequestUserIDResult params="+params.toString());
			strResult = GetJsonData.getInputStreamByPost(ConvertJsonToList.getMap(params.toString()), mContext, false);
			Logger.e("GetRequestParams ", "getRequestUserIDResult:"+" ,strResult= "+strResult);
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return strResult;
	}
	
	/**
	 * 发送带UserID的请求
	 * @param paramArr 参数
	 * @param UserID 登录用户名
	 * @param action 类型
	 * @param mContext 上下文
	 * @return
	 */
	public static String getRequestUserIDResult(String paramArr, String UserID,String action, Context mContext, boolean isold) {
		//Log.v("GetRequestParams ", "getRequestUserIDResult:"+" ,paramArr= "+paramArr+" ,UserID= "+UserID+" ,action= "+action);
		String strResult = null;
		try {
			String sign = getRequestSign(Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
			JSONObject params = new JSONObject();
			params.put("uuid", Uuid);
			params.put("platformCode", "Android");
			params.put("platformVersion", android.os.Build.VERSION.RELEASE);
			params.put("appVersion", ApplicationData.APPVERSION);
			params.put("UserID", UserID);
			params.put("action", action);
			params.put("params", paramArr.replace("+", "%2B"));
			params.put("sign", sign);
			params.put("cmdId",ApplicationData.getInstance().getCommenderID());
			params.put("cmdName",ApplicationData.getInstance().getCommenderName());
			Logger.e("GetRequestParams ", "getRequestUserIDResult params="+params.toString());
			strResult = GetJsonData.getInputStreamByPost(ConvertJsonToList.getMap(params.toString()), mContext, isold);
			Logger.e("getRequestUserIDResult", "strResult = " + strResult);
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return strResult;
	}

	/**
	 * 比分发送网络请求获取数据
	 * @param paramArr 请求参数，0 LotteryId  ； 1 LotteryId+lastTime
	 * @param action 请求数据类型，即哪个页面的请求
	 * @param mContext 上下文内容
	 * @return
	 */
	public static String getScoreRequestResult(String paramArr, String action, Context mContext) {
//		Log.v("GetRequestParams ", "getScoreRequestResult "+" paramArr= "+paramArr+" ,action= "+action);
		String strResult = null;
		try {
			String sign = getRequestSign(Uuid, "Android", android.os.Build.VERSION.RELEASE, ApplicationData.APPVERSION, paramArr, "8888");
			JSONObject params = new JSONObject();
			params.put("uuid", Uuid);
			params.put("platformCode", "Android");
			params.put("platformVersion", android.os.Build.VERSION.RELEASE);
			params.put("appVersion", ApplicationData.APPVERSION);
			params.put("action", action);
			if(paramArr.contains("lastTime")){//刷新
				//获取LotteryId
				String LotteryIdAll=paramArr.toString().split(",")[1];
				String LotteryId=LotteryIdAll.split(":")[1].replaceAll("\\}", " ").trim();
				params.put("LotteryId", LotteryId);
				//获取lastTime
				String lastTimeAll=paramArr.toString().split(",")[0];
				String lastTime=lastTimeAll.split(":")[1].replaceAll("\"", " ").trim();
				params.put("lastTime", lastTime);
			}else{//非刷新
				//获取LotteryId
				String LotteryId0=paramArr.toString().split(":")[1];
				String LotteryId=LotteryId0.replaceAll("\\}", " ").trim();
				params.put("LotteryId", LotteryId);
			}
			params.put("sign", sign);
			params.put("cmdId",ApplicationData.getInstance().getCommenderID());
			params.put("cmdName",ApplicationData.getInstance().getCommenderName());
			Logger.e("getScoreRequestResult ", "getScoreRequestResult params="+params.toString());
			strResult = GetScoreJsonData.getInputStreamByPost(ConvertJsonToList.getMap(params.toString()), mContext);
			Logger.e("getScoreRequestResult", "strResult = " + strResult);
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return strResult;
	}
}

